<%@ page contentType="text/html;charset=UTF-8" language="java" %>  
    <%@ page import="java.sql.*" %>
<!DOCTYPE html>  
<html lang="zh-CN">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>后厨主页</title>  
   
</head>  
<style>

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f2f5f4;
    color: #333;
}


.nav-links {
    list-style-type: none;
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    background-color: #809089;
}

.nav-links li {
    padding: 10px 20px;
}

.nav-links a {
    text-decoration: none;
    color: white;
    transition: color 0.3s;
}

.nav-links a:hover {
    color: #f2f5f4;
}


header {
    padding: 20px 0;
    background-color: #809089;
    color: #fff;
    text-align: center;
    position: relative;
}

header h1 {
    margin: 0;
    padding: 10px 0;
    font-weight: bold;
    text-transform: uppercase;
}


main {
    margin: 20px;
    padding: 30px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}


footer {
    background-color: #809089;
    color: #fff;
    text-align: center;
    padding: 10px 0;
    position: fixed;
    bottom: 0;
    width: 100%;
}


@media (max-width: 768px) {
    .nav-links {
        flex-direction: column;
    }



</style>
<script>
// 设置刷新间隔
setInterval(function() {
    location.reload();
}, 30000);
</script>
<body>  
<header>  
    <h1>后厨管理</h1>  
  
</header>  

<nav>
    <ul class="nav-links">
        <li><a href="javascript:void(0);" class="nav-link" data-target="#ingredients">菜品制作状态</a></li>
        <li><a href="javascript:void(0);" class="nav-link" data-target="#completion">菜品管理</a></li>
        <li><a href="javascript:void(0);" class="nav-link" data-target="#cancellations">已完成订单</a></li>
    </ul>
</nav>


<main>

    <section id="ingredients" class="section" style="display:none;">
    <%
    // 数据库连接信息
    String url = "jdbc:mariadb://10.220.140.102:3366/studb11";
    String user = "stua";
    String password = "zHZUA65r";
    

    // 加载并注册JDBC驱动
    Class.forName("org.mariadb.jdbc.Driver");
    
    %>
    
    <h2>等待制作菜品</h2>
 <table width="40%" class="table" border="1">
 <tr>  
        <th width="20%">等待制作</th>
        <th width="20%">总数量</th>
        <th width="20%">桌号</th>
        <th width="20%">操作</th>  
    </tr> 
    <% 

    Connection conn4 = DriverManager.getConnection(url, user, password);
    String restaurant_id = (String) session.getAttribute("restaurant_id");
    String sql4 = "SELECT name, SUM(amount) as total_quantity, GROUP_CONCAT(table_id) as table_numbers " +
            "FROM shoppingcar WHERE status = 'waiting' AND restaurant_id=? GROUP BY name";
try (PreparedStatement stmt = conn4.prepareStatement(sql4)) {
   stmt.setString(1, restaurant_id);
   ResultSet rs = stmt.executeQuery();
  
    while (rs.next()) {
    	%>
    	<tr onclick="fillform(this)" style="cursor:pointer">  
        <td><%=rs.getString("name")%></td>  
        <td><%=rs.getInt("total_quantity")%></td>
        <td><%=rs.getString("table_numbers")%></td>
    	<td>                
                <a href="save4.jsp?btn=del&name=<%=rs.getString("name") %>" onclick="return confirm('请确认是否开始制作？');">开始制作</a><br>   
        </td>
        <tr>
        <% 
        rs.close();
        }
    } %>
 </table>
 <br>
       <h2>制作中</h2>
 <table width="40%" class="table" border="1">
 <tr>  
        <th width="20%">制作中菜品</th>
        <th width="20%">总数量</th>
        <th width="20%">桌号</th>
        <th width="20%">操作</th>  
    </tr> 
    <% 
    // 查询making
    Connection conn1 = DriverManager.getConnection(url, user, password);
    String sql1 = "SELECT name, SUM(amount) as total_quantity, GROUP_CONCAT(table_id) as table_numbers " +
            "FROM shoppingcar WHERE status = 'making' AND restaurant_id=? GROUP BY name";
try (PreparedStatement stmt = conn1.prepareStatement(sql1)) {
   stmt.setString(1, restaurant_id); 
   ResultSet rs = stmt.executeQuery();
    while (rs.next()) {
    	%>
    	<tr onclick="fillform(this)" style="cursor:pointer">  
        <td><%=rs.getString("name")%></td>  
        <td><%=rs.getInt("total_quantity")%></td>
        <td><%=rs.getString("table_numbers")%></td>
    	<td>                
                <a href="save1.jsp?btn=del&name=<%=rs.getString("name") %>" onclick="return confirm('请确认是否出菜？');">出菜</a><br>   
        </td>
        <tr>
        <% 
        rs.close();
        }
    } %>
 </table>
 <br>
 
 <h2>制作完成</h2>
 <table width="40%" class="table" border="1">
<tr>  
        <th width="20%">已出菜菜品</th>
        <th width="20%">总数量</th>
        <th width="20%">桌号</th>
        <th width="20%">操作</th>  
    </tr> 
    <% 
    // 查询ready
    Connection conn2 = DriverManager.getConnection(url, user, password);
    String sql2 = "SELECT name, SUM(amount) as total_quantity, GROUP_CONCAT(table_id) as table_numbers " +"FROM shoppingcar WHERE status = 'ready'  AND restaurant_id=" + restaurant_id;
    try (PreparedStatement stmt = conn2.prepareStatement(sql2)){
    ResultSet rs = stmt.executeQuery();
    while (rs.next()) {
    	%>
    	<tr onclick="fillform(this)" style="cursor:pointer">  
        <td><%=rs.getString("name")%></td>  
        <td><%=rs.getInt("total_quantity")%></td>
        <td><%=rs.getString("table_numbers")%></td>
    	<td>                
                <a href="save2.jsp?btn=del&name=<%=rs.getString("name") %>" onclick="return confirm('请确认是否上菜？');">上菜</a><br>   
        </td>
        <tr>
        <% 
        rs.close();
        }
    } %>
</table>
<br>

    </section>
    <section id="completion" class="section" style="display:none;">
   
        <table width="40%" class="table" border="1">
        <tr>  
               <th width="20%">库存不足菜品</th>
               <th width="20%">操作</th>  
           </tr> 
           <% 
           // 查询所有库存为0
           Connection conn = DriverManager.getConnection(url, user, password);
           String sql="SELECT name FROM dishes WHERE stock_quantity = '0'";
           try (PreparedStatement stmt = conn.prepareStatement(sql)){
           ResultSet rs = stmt.executeQuery();
           while (rs.next()) {
           	%>
           	<tr onclick="fillform(this)" style="cursor:pointer">  
               <td><%=rs.getString("name")%></td>  
           	<td>                
                       <a href="save.jsp?btn=del&name=<%=rs.getString("name") %>" onclick="return confirm('请确认是否下架当前食材？');">下架</a><br>   
               </td>
               <tr>
               <% 
               rs.close();
               }
           } %>
        </table>
        <br>
      
      
      
     
 <table width="40%" class="table" border="1">
        <tr>  
               <th width="20%">已下架菜品</th>
               <th width="20%">操作</th>  
           </tr> 
           <% 
           // 查询已下架菜品
           Connection conn6 = DriverManager.getConnection(url, user, password);
           String sql6="SELECT name FROM dishes WHERE stock_quantity = '//'";
           try (PreparedStatement stmt = conn6.prepareStatement(sql6)){
           ResultSet rs = stmt.executeQuery();
           while (rs.next()) {
           	%>
           	<tr onclick="fillform(this)" style="cursor:pointer">  
               <td><%=rs.getString("name")%></td>  
           	<td>                
                <a href="save5.jsp?btn=del&name=<%=rs.getString("name") %>" onclick="return confirm('请确认是否上新当前食材？');">上新</a><br>   
               </td>
               <tr>
               <% 
               rs.close();
               }
           } %>
        </table>
        <br>
      
    </section>
    <section id="cancellations" class="section" style="display:none;">
        <%
     
    
        if (restaurant_id == null) {
         
            out.println("<script>alert('无法获取餐馆信息，请重新登录！');location.href='login.jsp';</script>");
        } else {
            try {
             
                Connection conn5 = DriverManager.getConnection(url, user, password);
                String sql5 = "SELECT name, order_time " + "FROM shoppingcar WHERE status = '已完成' AND restaurant_id=?";

                try (PreparedStatement stmt = conn5.prepareStatement(sql5)) {
                    stmt.setString(1, restaurant_id); 
                    ResultSet rs = stmt.executeQuery();
                    out.println("<h2>已完成订单</h2>");
                    out.println("<table border='1'>");
                    out.println("<tr><th>菜品名称</th><th>时间</th></tr>");

                    while (rs.next()) {
                        String dishName = rs.getString("name");
                        String order_time = rs.getString("order_time");
                        out.println("<tr><td>" + dishName + "</td><td>" + order_time + "</td></tr>");
                    }
                    out.println("</table><br>");
                }
            } catch (SQLException e) {
            
                e.printStackTrace();
            }
        }
        %>
    </section>
</main>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const navLinks = document.querySelectorAll('.nav-link');

        navLinks.forEach(link => {
            link.addEventListener('click', function () {
              
                document.querySelectorAll('.section').forEach(section => {
                    section.style.display = 'none';
                });

              
                const target = document.querySelector(this.getAttribute('data-target'));
                if (target) {
                    target.style.display = 'block';
                }
            });
        });
    });
</script>
<footer>  

    <p>后厨管理系统</p>  
</footer>  

  
</body>  
</html>